ZipOutExtended.java
/**
* ZipOutExtended.java
*
* @author Artinsoft
*/
import java.io.*;
import java.util.zip.*;
public class ZipOutExtended extends java.util.zip.ZipOutputStream {
/** Extends the zip output stream class in order to provide aditional funcionality
* for compressing directories.
*
* @param outname the name of the output zip file
* @param dirName the name of the directory to be compressed
*/
public ZipOutExtended(String outname, String dirName) throws IOException{
super(new FileOutputStream(outname+".zip"));
zipDir(dirName);
}
private void zipDir(String dirName) throws IOException{
File dirObj = new File (dirName);
if (dirObj.isDirectory())
{
File [] fileList = dirObj.listFiles();
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
zipDir (fileList[i].getPath());
}
else if (fileList[i].isFile()) {
String path = fileList[i].getPath();
BufferedInputStream file = new BufferedInputStream(new FileInputStream(path));
// put the Zip Entry
System.out.println("Including file: " + path);
this.putNextEntry(new ZipEntry(path));
//write the data
byte[] data = new byte[1024];
int byteCount;
while ((byteCount = file.read(data, 0, 1024)) > -1) {
this.write(data, 0, byteCount);
}
this.closeEntry();
file.close();
}
}
}
else{
throw new IOException("Directory "+ dirName+" does not exist.");
}
}
}